package com.ruoyi.pos.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pos.domain.Goods;
import com.ruoyi.pos.mapper.ColorMapper;
import com.ruoyi.pos.domain.Color;
import com.ruoyi.pos.service.IColorService;

/**
 * colorService业务层处理
 * 
 * @author wzh
 * @date 2021-04-19
 */
@Service
public class ColorServiceImpl implements IColorService 
{
    @Autowired
    private ColorMapper colorMapper;

    /**
     * 查询color
     * 
     * @param id colorID
     * @return color
     */
    @Override
    public Color selectColorById(Long id)
    {
        return colorMapper.selectColorById(id);
    }

    /**
     * 查询color列表
     * 
     * @param color color
     * @return color
     */
    @Override
    public List<Color> selectColorList(Color color)
    {
        return colorMapper.selectColorList(color);
    }

    /**
     * 新增color
     * 
     * @param color color
     * @return 结果
     */
    @Transactional
    @Override
    public int insertColor(Color color)
    {
        int rows = colorMapper.insertColor(color);
        insertGoods(color);
        return rows;
    }

    /**
     * 修改color
     * 
     * @param color color
     * @return 结果
     */
    @Transactional
    @Override
    public int updateColor(Color color)
    {
        colorMapper.deleteGoodsByCoid(color.getId());
        insertGoods(color);
        return colorMapper.updateColor(color);
    }

    /**
     * 批量删除color
     * 
     * @param ids 需要删除的colorID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteColorByIds(Long[] ids)
    {
        colorMapper.deleteGoodsByCoids(ids);
        return colorMapper.deleteColorByIds(ids);
    }

    /**
     * 删除color信息
     * 
     * @param id colorID
     * @return 结果
     */
    @Override
    public int deleteColorById(Long id)
    {
        colorMapper.deleteGoodsByCoid(id);
        return colorMapper.deleteColorById(id);
    }

    /**
     * 新增goods信息
     * 
     * @param color color对象
     */
    public void insertGoods(Color color)
    {
        List<Goods> goodsList = color.getGoodsList();
        Long id = color.getId();
        if (StringUtils.isNotNull(goodsList))
        {
            List<Goods> list = new ArrayList<Goods>();
            for (Goods goods : goodsList)
            {
                goods.setCoid(id);
                list.add(goods);
            }
            if (list.size() > 0)
            {
                colorMapper.batchGoods(list);
            }
        }
    }
}
